//
//  XZChangePwdVC.m
//  Teecloud
//
//  Created by 贵州贵农网络科技有限公司 on 2018/7/16.
//  Copyright © 2018年 小志. All rights reserved.
//

#import "XZChangePwdVC.h"

@interface XZChangePwdVC ()<UITextFieldDelegate>
//原密码框
@property (weak, nonatomic) IBOutlet UITextField *originalTextField;
//现输入密码框
@property (weak, nonatomic) IBOutlet UITextField *newsTextField;

@end

@implementation XZChangePwdVC
#pragma mark - LifeCycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改密码";
    
}
#pragma mark - 原密码眼睛按钮
- (IBAction)originalPwdEyeButtonClick:(UIButton *)sender {
    sender.selected = !sender.selected;
    [self.originalTextField setSecureTextEntry:!sender.selected];
}
#pragma mark - 现输入密码眼睛按钮
- (IBAction)newPwdEyeButtonClick:(UIButton *)sender {
    sender.selected = !sender.selected;
    [self.newsTextField setSecureTextEntry:!sender.selected];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if ([string isEqualToString:@"\n"]){
        return YES;
    }
    NSString * aString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if (self.newsTextField == textField)
    {
        
        if ([aString length] > 12){
            [MBProgressHUD showErrorOrSuc:@"新密码长度不能大于12" toView:self.view];
            return NO;
        }
    }
    return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (self.newsTextField == textField) {
        if (textField.text.length < 6) {
         [MBProgressHUD showErrorOrSuc:@"新密码长度不能小于6" toView:self.view];
        }
    }
}
#pragma mark - 确认修改按钮
- (IBAction)confirmButtonClick:(UIButton *)sender {
    /**判断原密码是否为空*/
    if (kIsEmptyString(self.originalTextField.text)) {
        [MBProgressHUD showErrorOrSuc:@"请输入原密码" toView:self.view];
        return;
    }
    /**判断设置的新密码是否为空*/
    if (kIsEmptyString(self.newsTextField.text)) {
        [MBProgressHUD showErrorOrSuc:@"请输入新密码" toView:self.view];
        return;
    }
    
    /**判断设置的新密码长度是否小于6*/
    if (self.newsTextField.text.length < 6) {
        [MBProgressHUD showErrorOrSuc:@"新密码长度不能小于6" toView:self.view];
        return;
    }
    NSDictionary *dict= @{
                          @"from":FROM_IOS,
                          @"busiOrgId":[XGMArchiverTools cacheForKey:BUSIORGID],
                          @"userId":[XGMArchiverTools cacheForKey:USER_UserId],
                          @"loginFinger":[XGMArchiverTools cacheForKey:USER_identifierNumber],
                          @"fieldName":@"Password",
                          @"fieldValue":self.newsTextField.text,
                          @"verifyCode":self.originalTextField.text
                          };
    __weak typeof(self)weakSelf = self;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HJNetwork POSTWithURL:[NSString stringWithFormat:@"ajax-api.ashx?$c=TeeyunApp&$m=UpdateUserInfo&$v=%@",VERSION] parameters:dict cachePolicy:HJCachePolicyIgnoreCache callback:^(id responseObject, NSError *error) {
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        if (!error) {
            if ([[responseObject objectForKey:RESULT_NAME_Success] boolValue]) {
                [MBProgressHUD showErrorOrSuc:@"密码重置成功" toView:weakSelf.view];
                [weakSelf.navigationController popViewControllerAnimated:true];
            }else{
                [MBProgressHUD showErrorOrSuc:[responseObject objectForKey:RESULT_NAME_Message] toView:weakSelf.view];
            }
        }else{
            NSLog(@"%@",error);
        }
    }];
}

@end
